package com.zlsy.thread.threadlocal;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


/**
 * @author zhouliang
 * @date 2020/07/11
 * <p>
 * threadLocal的第二种用法，避免参数传递的麻烦。
 **/
public class ThreadLocalNormalUsage06 {


    public static void main(String[] args) {
        Service1 service1 = new Service1();
        service1.process();
    }


}

class Service1 {

    public void process() {
        User user = new User("张三");
        UserContextHolder.threadLocal.set(user);

        new Service2().process();
    }

}

class Service2 {
    public void process() {
        User user = UserContextHolder.threadLocal.get();
        System.out.println("service2 ： " + user.getName());
        new Service3().process();
    }
}

class Service3 {
    public void process() {
        User user = UserContextHolder.threadLocal.get();
        System.out.println("service3 ： " + user.getName());
    }
}

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
class User {
    private String name;
}

class UserContextHolder {
    public static ThreadLocal<User> threadLocal = new ThreadLocal<>();
}